<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>群组管理</el-breadcrumb-item>
      <el-breadcrumb-item>群组管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 用户列表主体部分 -->
    <!-- 卡片视图区 -->
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索区域 -->
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGroupList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGroupList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 用户列表 border边框 stripe隔行变色-->
      <el-table :data="groupList" border="" stripe="">
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="群组名" prop="groupName"></el-table-column>
        <el-table-column label="所属租户" prop="tenantName"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="ViewMembers(scope.row.gid, scope.row.groupName)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGroup(scope.row.gid)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
  
<script>
import Vue from "vue";
export default {
  created() {
    this.tenantname = window.sessionStorage.getItem("name");
    this.getGroupList();
  },

  data() {
    return {
      //查询信息实体
      queryInfo: {
        query: "", //查询信息
        pageNum: 1, //当前页
        pageSize: 5, //每一页最大数
      },
      groupList: [], //群组列表
      total: 0, //总记录数
      tenantname: "",
    };
  },
  methods: {
    //获取所有群组
    async getGroupList() {
      await this.$http({
        method: "post",
        url: "/GroupInTenantFuzzy",
        data: {
          query: this.queryInfo.query,
          pageNum: this.queryInfo.pageNum,
          pageSize: this.queryInfo.pageSize,
          name: this.tenantname,
        },
      }).then((response) => {
        this.groupList = response.data.data.list;
        this.total = response.data.data.total;
      });
    },

    //最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getGroupList();
    },

    //pageNum触发的动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getGroupList();
    },

    //根据主键删除用户
    async deleteGroup(gid) {
      const confirmResult = await this.$confirm(
        "此操作将永远删除群组，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        //取消
        return this.$message.info("已取消删除");
      }
      await this.$http({
        method: "post",
        url: "/deleteGroup?gId=" + gid,
      }).then((res) => {
        if (res.data.code == "666") {
          this.$message.success(res.data.message);
          this.getGroupList();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },

    //查看群组成员
    ViewMembers(gid, groupname) {
      window.sessionStorage.setItem("tenantname", this.tenantname);
      window.sessionStorage.setItem("gid", gid);
      window.sessionStorage.setItem("groupname", groupname);

      this.$router.push({
        path: "/groupmembers",
        query: {
          name: "租户管理群组",
        },
      }); //页面路由跳转
    },
  },
};
</script>
  
<style lang="less" scoped>
/* 面包屑样式 */
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>
